/**
 * This software is licensed under the MIT license. 
 */
package de.kune.sessionxs.matcher;

import javax.servlet.http.HttpServletRequest;

import de.kune.sessionxs.matcher.context.SessionMatcherContext;
import de.kune.sessionxs.matcher.context.StringValueContext;

/**
 * Provides client IP address-based matching. This matcher makes sure that all
 * requests of a session originate from the same IP address.
 * 
 * @author Alexander Kune
 * 
 */
public class ClientIPAddressMatcher extends AbstractSessionMatcher implements SessionMatcher {

	@Override
	public SessionMatcherContext init(HttpServletRequest request) {
		return new StringValueContext(request.getRemoteAddr());
	}

	@Override
	public boolean matches(SessionMatcherContext context,
			HttpServletRequest request) {
		if (context instanceof StringValueContext) {
			return ((StringValueContext) context).getValue().equals(
					request.getRemoteAddr());
		} else {
			throw new IllegalArgumentException("Context must be of class ["
					+ StringValueContext.class.getName() + "].");
		}
	}

}
